import 'package:boss/common/common.dart';
import 'package:boss/main.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

class ForgetPasswordProvider extends ChangeNotifier {
  String phone;
  String code;
  String password;

  setPhone(String value) {
    phone = value;
    notifyListeners();
  }

  setCode(String value) {
    code = value;
    notifyListeners();
  }

  setPassword(String value) {
    password = value;
    notifyListeners();
  }

  forgetPassword(BuildContext context) async {
    if (!RegUtil.testPhone(phone)) {
      toast('请输入正确的手机号');
      return;
    }

    if (!RegUtil.testCode(code)) {
      toast('请输入4位验证码');
      return;
    }

    if (!RegUtil.testPwd(password)) {
      toast('请输入6-16位含数字字母密码');
      return;
    }

    try {
      showLoadingToast();
      var result = await api.updatePassword({
        'password': StringUtil.md5(password),
        'phone': phone,
        'verifyCode': code,
      });
      if (!result.successful()) {
        toast(result.msg);
        return;
      }

      toast('重置密码成功');

      Navigator.pop(context);
    } catch (e) {
      toast(Constant.httpServeErrText);
    } finally {
      hideLoadingToast();
    }
  }
}
